<template>
  <list-card>
    <template v-slot:search>
      <el-form ref="queryForm" :model="queryParams" inline size="small">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="queryParams.name" clearable placeholder="请输入字典名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="字典编码" prop="code">
          <el-input v-model="queryParams.code" clearable placeholder="请输入字典编码"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="字典状态">
            <el-option v-for="item in localDict.system_status" :key="item.value" :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:operate>
      <el-button v-has-permi="['config:dict:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <el-button v-has-permi="['config:dict:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
<!--      <el-button v-has-permi="['config:dict:export']" icon="el-icon-download" plain size="mini" type="warning"-->
<!--                 @click="handleExport">导出-->
<!--      </el-button>-->
      <el-button v-has-permi="['config:dict:remove']" icon="el-icon-refresh" plain size="mini" type="danger"
                 @click="handleRefreshCache">刷新缓存
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </template>
    <template v-slot:content>
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column align="center" label="字典名称" prop="name" show-overflow-tooltip/>
        <el-table-column align="center" label="字典编码" prop="code" show-overflow-tooltip/>
        <el-table-column align="center" label="状态" prop="status">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.system_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" prop="remark" show-overflow-tooltip/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template v-slot:default="scope">
            <el-button v-has-permi="['config:dict:edit']" icon="el-icon-edit" size="mini" type="text"
                       @click="handleEdit(scope.row)">修改
            </el-button>
            <el-button v-has-permi="['config:dict:remove']" icon="el-icon-delete" size="mini" type="text"
                       @click="handleDelete(scope.row)">删除
            </el-button>
            <el-button v-has-permi="['config:dict:edit']" icon="el-icon-s-tools" size="mini" type="text"
                       @click="handleConfig(scope.row)">字典配置
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageParam.total > 0" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                  :total="pageParam.total" @pagination="getList"/>
    </template>
    <model-card ref="model" @ok="getList"/>
    <item-card ref="item" @ok="getList"/>
  </list-card>
</template>

<script>
import {getStaticData} from "@/api/common";
import {clearCache, getOne, list, remove} from "../../../api/base";
import ItemCard from "./components/ItemCard.vue";
import ModelCard from "./components/ModelCard.vue";
import moment from "moment";

export default {
  name: "Dict",
  components: {ItemCard, ModelCard},
  // dicts: ['sys_normal_disable'],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      baseUrl: '/config/dict',
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {
        name: undefined,
        code: undefined,
        status: undefined
      },
      localDict: {
        system_status: []
      },
      // 显示搜索条件
      showSearch: false,
    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    async getLocalDict() {
      await this.getStaticData('system-status').then(res => {
        this.localDict.system_status = res.data
      })
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
          this.dataList = response.rows;
          this.pageParam.total = response.total;
          this.loading = false;
        }
      );
    },
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add()
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.$refs.model.edit(row.id)
    },
    handleConfig(row) {
      this.$refs.item.visible = true;
      this.$refs.item.title = row.name + '配置'
      this.$refs.item.dictType = row.code
      this.$refs.item.resetQuery()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？').then(() => {
        remove(this.baseUrl, dictIds).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      clearCache(this.baseUrl).then(() => {
        this.$modal.msgSuccess("刷新成功");
        this.$store.dispatch('dict/cleanDict');
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.baseUrl + '/export', {
        ...this.queryParams
      }, `数据字典_${moment().format('YYYYMMDD_HHmmss')}.xlsx`)
    },
  }
};
</script>
